<!-- BEGIN PAGE HEADING ROW -->
<div class="row">
	<div class="col-lg-12">
		<!-- BEGIN BREADCRUMB -->
		<div class="breadcrumbs">
			<ul class="breadcrumb">
				<li class="active">
					<a href="javascript:;">Trang chủ</a>
				</li>
			</ul>
		</div>
		<!-- END BREADCRUMB -->

		<div class="page-header">
			<h3>Danh Sách Danh Mục</h3>
		</div>

		<?php echo $this->element('layout/layout-options')?>

	</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<!-- END PAGE HEADING ROW -->


<div class="row">
	<div class="col-lg-12">
	<?php echo $this->Session->flash(); ?>
		<table class="table table-bordered table-hover tc-table table-primary footable" data-page-size="2">
			<thead>
				<tr>
					<th>STT</th>
					<th>Tên</th>
					<th>Danh Mục Con</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php if(count($items) > 0):?>
				<?php foreach($items as $k => $v):?>
				<tr>
					<td style="width:5%" align="center">
						<a href="#" data-pid="<?= $v[$als]['id']?>" class="_sort" style="display:block;"><?php echo $v[$als]['sort'];?></a>
						<input type="text" class="hide" style="width:32px; text-align: center;" data-sort="<?= $v[$als]['sort'];?>" value="<?= $v[$als]['sort'];?>" />
					</td>

					<td style="width:25%"><?php echo $v[$als]['name'];?></td>

					<td>
						<div class="btn-margin-bottom-5">
							<a href="<?= $this->webroot.'admin' ?>/categories/add/<?= $v[$als]['id']?>" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i>Thêm mới</a>
						</div>
						<?php if(count($v[$als]['childs']) > 0):?>
						<table class="table table-bordered table-hover tc-table">
							<thead>
								<tr>
									<th>STT</th>
									<th>Tên</th>
									<th></th>
								</tr>
							</thead>
							<tbody>

							<?php foreach($v[$als]['childs'] as $child): ?>
							<tr>
								<td style="width:5%" align="center"><?= $child[$als]['sort']?></td>
								<td><?= $child[$als]['name']?></td>
								<td class="col-medium center hidden-xs hidden-sm">
									<div class="action-buttons">
										<a href="<?= $this->webroot.'admin' ?>/products/index/<?= $v[$als]['id']?>/<?= $child[$als]['id']?>" class="tooltip-purple" data-placement="top" data-rel="tooltip" title="" data-original-title="Xem sản phẩm"><i class="fa fa-university bigger-130 text-purple" ></i></a>
										<a href="<?= $this->webroot.'admin' ?>/categories/images/<?= $v[$als]['id']?>/<?= $child[$als]['id']?>" class="tooltip-primary" data-placement="top" data-rel="tooltip" title="" data-original-title="Xem hình ảnh"><i class="fa fa-picture-o bigger-130 text-primary"></i></a>
										<a href="<?= $this->webroot.'admin' ?>/categories/add/<?= $v[$als]['id']?>/<?= $child[$als]['id']?>" class="tooltip-green" data-placement="top" data-rel="tooltip" title="" data-original-title="Sửa"><i class="fa fa-edit bigger-130 text-green"></i></a>
										<a href="<?= $this->webroot.'admin' ?>/categories/delete/<?= $v[$als]['id']?>/<?= $child[$als]['id']?>" class="tooltip-red" data-placement="top" data-rel="tooltip" title="" data-original-title="Xóa"><i class="fa fa-trash-o bigger-130 text-red"></i></a>
									</div>
								</td>
							</tr>
							<?php endforeach; ?>
							</tbody>
						</table>
						<?php endif; ?>
					</td>

					<td class="col-medium center hidden-xs hidden-sm">
						<div class="action-buttons">
							<a href="<?= $this->webroot.'admin' ?>/categories/images/0/<?= $v[$als]['id']?>" class="tooltip-primary" data-placement="top" data-rel="tooltip" title="" data-original-title="Xem hình ảnh"><i class="fa fa-picture-o bigger-130 text-primary"></i></a>
							<a href="<?= $this->webroot.'admin' ?>/categories/add/0/<?= $v[$als]['id']?>" class="tooltip-green" data-placement="top" data-rel="tooltip" title="" data-original-title="Sửa"><i class="fa fa-edit bigger-130 text-green"></i></a>
						</div>
					</td>
				</tr>
				<?php endforeach; else:?>
				<tr>
					<td colspan="4"><div class="note note-primary">Chưa có danh mục</div></td>
				</tr>
				<?php endif; ?>
			</tbody>

		</table>


	</div><!-- /.col-lg-12 -->
</div><!-- /.row -->

<?php
	echo $this->Html->script('plugins/bootbox/bootbox.min');
	echo $this->Html->script('functions');
?>

<script>
$( document ).ready(function($) {

	showSort($('._sort'));

	// Xoa danh muc
	$('.action-buttons .tooltip-red').click(function(event){
		event.preventDefault();
		var href =  $(this).attr('href');
		bootbox.confirm("<b>Nếu xóa danh mục này thì tất cả sản phẩm của danh mục này cũng bị xóa.</b><br /> Bạn có chắc chắn muốn xóa danh mục này không?", function(result) {
			if(result) {
				window.location.href = href;
			}
		});
		return false;
	});
});

function showSort($hml) {
	$hml.click(function(event){
		event.preventDefault();

		$('.alert').remove();

		var $span = $(this);

		var value = $span.html();

		var $input = $span.next();

		checkInput($input, value);
		$input.data('sort', value).val(value).attr('value', value).removeClass('hide');
		$span.hide();

		$input.focus();

		$input.focusout(function(event){
			event.preventDefault();
			$('.alert').remove();

			postAjax('<?php echo $this->webroot; ?>admin/categories/changeSort', {id:$span.data('pid'), sort:$input.val()}, function(resp){
				var msg = 'Sửa thứ tự thất bại';
				if(resp.code == 'success'){
					$span.html($input.val());
					msg = 'Sửa thứ tự thành công';
					$input.addClass('hide');
					$span.show();
				}

				var $msg = '<div class="alert alert-'+resp.code+'"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Thành Công:</strong><br> '+msg+'</p></div>';
				$input.closest('table').before($msg);
			}, 'json', true);
		});
	});
}



/**
 * check als
 */
function checkInput($input, value) {
	$input.unbind().keyup(function(){
        if (this.value.match(/[^0-9]/g)) {
            this.value = value;
        }
    });
}


</script>
